package xyz.yq56.easytool.utils.date;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 考虑维护一个成员变量,这样可以不用老是输入参数date,直接获取对应实例就可以完成各种操作.当前水平不够,还无法考虑请并发问题,暂不实现
 *
 * @author yi qiang
 * @date 2021/10/1 18:28
 */
public class DateUtils {

    private DateUtils() {
    }

    public static Date addDate(long time) {
        return new Date(System.currentTimeMillis() + time);
    }

    public static Date add(Date date, int field, int value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, value);
        return calendar.getTime();

    }

    public static Date getNow() {
        return new Date();
    }

    /**
     * 获取当月的最后一天(时间点还是当前时间)
     *
     * @param date 天
     * @return 当月最后一天
     */
    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getNextMonthDay(date));
        calendar.set(Calendar.DATE, 0);

        Calendar result = Calendar.getInstance();
        result.setTime(date);
        result.set(Calendar.DATE, calendar.get(Calendar.DATE));
        return result.getTime();
    }


    /**
     * 获取下个月的当天(时间点还是当前时间)
     *
     * @param date 当天
     * @return 获取下个月的当天
     */
    public static Date getNextMonthDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 1);
        return cal.getTime();
    }

    /**
     * 将时间点置为 00:00:000
     *
     * @param date 日期
     * @return 替换后的时间
     */
    public static Date toZeroTime(Date date) {
        Calendar result = Calendar.getInstance();
        result.setTime(date);
        result.set(Calendar.MINUTE, 0);
        result.set(Calendar.HOUR_OF_DAY, 0);
        result.set(Calendar.SECOND, 0);
        result.set(Calendar.MILLISECOND, 0);
        return result.getTime();
    }

    /**
     * 将时间点置为 23:59:59
     *
     * @param date 日期
     * @return 替换后的时间
     */
    public static Date toLastTime(Date date) {
        Calendar result = Calendar.getInstance();
        result.setTime(date);
        result.set(Calendar.HOUR_OF_DAY, 23);
        result.set(Calendar.SECOND, 59);
        result.set(Calendar.MILLISECOND, 59);
        result.set(Calendar.MINUTE, 59);
        return result.getTime();
    }

    /**
     * 获取对应时区的时间字符串
     *
     * @param date     时间
     * @param timeZone 时区
     * @return 对应时区的时间字符串
     */
    public static String getTimeWithTimeZone(Date date, TimeZone timeZone) {
        Calendar result = Calendar.getInstance();
        result.setTime(date);
        SimpleDateFormat simpleDateFormat = DateFormatter.standardFormatter();
        simpleDateFormat.setTimeZone(timeZone);
        return simpleDateFormat.format(result.getTime());
    }

    public static void main(String[] args) {
        System.out.println(new Date());
        System.out.println(getTimeWithTimeZone(new Date(), TimeZone.getTimeZone("GMT+07:00")));
        System.out.println(toZeroTime(new Date()));
        System.out.println(toLastTime(new Date()));
        System.out.println(getNextMonthDay(new Date()));
        System.out.println(getLastDayOfMonth(new Date()));
        System.out.println(add(new Date(), Calendar.MINUTE, 2));
    }

}
